//
//  LGFTitleTextField.swift
//  MedicalUnion
//
//  Created by apple on 2019/8/13.
//  Copyright © 2019 William_Xue. All rights reserved.
//

import UIKit

protocol LGFTitleTextFieldDelegate: class {
    func titleTextFieldEdit(view: LGFTitleTextField, isEnd: Bool) -> Void
}

class LGFTitleTextField: UIView, UITextFieldDelegate {
    
    weak var delegate: LGFTitleTextFieldDelegate?
    
    fileprivate var labStar: UILabel!
    var labName: UILabel!
    var textField: UITextField!
    var line: UIView!
    
    var needText: String! = "*"
    var textFont: UIFont!// 字体大小
    var isNeedInput: Bool!// 是否必填
    var titleStr: String!// title
    var placeholderStr: String?// placeholder
    var lineLeft: CGFloat! {// 底部线左侧距离
        didSet {
            if line != nil {
                line.transform = CGAffineTransform.identity
                line.transform = CGAffineTransform.init(translationX: lineLeft.lgf_W(), y: 0)
            }
        }
    }
    
    // 是否可编辑
    var isCanEdit: Bool! {
        didSet {
            self.isUserInteractionEnabled = isCanEdit
        }
    }
    class func lgf(text: String,
                   placeholder: String,
                   font: UIFont,
                   need: Bool,
                   canEdit: Bool,
                   lineLeft: CGFloat) -> LGFTitleTextField {
        return lgf(needText: "*", text: text, placeholder: placeholder, font: font, need: need, canEdit: canEdit, lineLeft: lineLeft)
    }
    class func lgf(needText: String,
                   text: String,
                   placeholder: String,
                   font: UIFont,
                   need: Bool,
                   canEdit: Bool,
                   lineLeft: CGFloat) -> LGFTitleTextField {
        let view: LGFTitleTextField = LGFTitleTextField.init()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.lgf_ViewName = text
        view.isNeedInput = need
        view.titleStr = text
        view.textFont = font
        view.needText = needText
        if canEdit { view.placeholderStr = placeholder }
        view.lineLeft = lineLeft
        view.backgroundColor = UIColor.white
        view.configView()
        view.isCanEdit = canEdit
        return view
    }
    
    func configView() {
        // 必选 *
        labStar = UILabel()
        labStar.textColor = UIColor.init(lgf_HexString: "F6432E")
        labStar.font = textFont
        labStar.text = needText
        labStar.isHidden = !isNeedInput
        addSubview(labStar)
        labStar.snp.makeConstraints { (make) in
            make.left.equalTo(5.lgf_W())
            make.centerY.equalToSuperview().offset(2)
        }
        
        // 标题
        labName = UILabel.init()
        labName.textColor = UIColor.init(lgf_HexString: "111111")
        labName.font = textFont
        labName.textAlignment = .left
        labName.text = titleStr
        addSubview(labName)
        labName.snp.makeConstraints { (make) in
            make.left.equalTo(15.lgf_W())
            make.centerY.equalToSuperview()
            make.width.lessThanOrEqualTo(150.lgf_W())
        }
        
        // 输入框
        textField = UITextField()
        textField.borderStyle = UITextField.BorderStyle.none
        textField.attributedPlaceholder = NSAttributedString.init(string: "", attributes: [NSAttributedString.Key.font:textFont! ,NSAttributedString.Key.foregroundColor:UIColor.init(lgf_HexString: "a5a5a5")!])
        textField.textColor = UIColor.init(lgf_HexString: "878787")
        textField.font = textFont
        textField.adjustsFontSizeToFitWidth = true
        textField.minimumFontSize = 9
        textField.textAlignment = .left
        textField.contentVerticalAlignment = .center
        textField.clearButtonMode=UITextField.ViewMode.whileEditing
        textField.delegate = self
        textField.placeholder = placeholderStr
        addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(labName.snp.right).offset(15.lgf_W())
            make.right.equalToSuperview().offset(-15.lgf_W())
            make.centerY.equalTo(labName.snp.centerY)
        }

        // 底部线
        line = UIView.init()
        line.backgroundColor = UIColor.init(lgf_HexString: "F1F2F4")
        addSubview(line)
        line.transform = CGAffineTransform.init(translationX: lineLeft.lgf_W(), y: 0)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        textFieldEdit(textField, false)
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        textFieldEdit(textField, true)
    }

    func textFieldEdit(_ textField: UITextField, _ isEnd: Bool) -> Void {
        self.delegate?.titleTextFieldEdit(view: self, isEnd: isEnd)
    }
}
